#include "include/common.h"
#include <list>
#include "include/memory/memory_pool.h"
#include "include/gc/gc_factory.h"

#include "src/vm/share/classfile/classFileStream.h"
#include "src/vm/share/utilities/top.h"
#include "src/vm/share/classfile/classLoader.h"
#include "src/cpu/os/bytes.h"

using namespace std;

int main(){

    stringStream st;
    st.print_raw("hello");
    st.print_raw(".class");

    INFO_PRINT("===> %s \n", st.as_string());

    ClassLoader::initialize();
    ClassLoader::load_classfile("com/lemon/jvm/example/HelloWorld");

    u2 b = 1;
    u2 c = Bytes::swap_u2(b);
    INFO_PRINT("====>%x\n", c);

    u4 d = 1;
    u4 f = Bytes::swap_u4(d);
    INFO_PRINT("====>%x\n", f);


    u8 j = 1;
    u8 h = Bytes::swap_u8(j);
    u8 g = (((u8)Bytes::swap_u4(1))<<32) | 0;
    g= ((u8)1) << 62;
    INFO_PRINT("====>%lu lu\n", h,g);


#if !defined(LINUX) && !defined(_ALLBSD_SOURCE)
    INFO_PRINT("He\n");
#endif

    return 0;
}

int main0() {

    MemoryPool *mem_pool = new MemoryPool();

    MemoryChunk *mem_chunk = mem_pool->new_chunk(78 * 2);

    // 模拟分的内存超过Chunk的大小
//    mem_chunk->malloc(80);
//    mem_chunk->malloc(81);

    // 模拟内存足够，能分到内存
    MemoryCell *cell1 = mem_chunk->malloc(8);
    MemoryCell *cell2 = mem_chunk->malloc(16);
    MemoryCell *cell3 = mem_chunk->malloc(32);
    MemoryCell *cell4 = mem_chunk->malloc(16);

    *((char *)cell3->ptr()) = 'a';
    *((char *)cell1->ptr()) = 'c';
    INFO_PRINT("%X=====>%c, %c\n", cell3->ptr(), *((char *)cell3->ptr()), *((char *)cell1->ptr()));

    // 模拟GC后因内存碎片，分不到内存
    mem_chunk->malloc(40);

    INFO_PRINT("%X=====>%c, %c\n", cell3->ptr(), *((char *)cell3->ptr()), *((char *)cell1->ptr()));

    // 模拟出发第二次GC
//    mem_chunk->malloc(40);
//
//    INFO_PRINT("%X=====>%c, %c\n", cell3->ptr(), *((char *)cell3->ptr()), *((char *)cell1->ptr()));

   // mem_pool->print_chunks();

    delete mem_pool;

    return 0;
}
